package com.zxmr.hospital.customer.action;

import com.alibaba.fastjson.JSON;
import com.zxmr.action.Request;
import com.zxmr.action.Response;
import com.zxmr.action.ResponsePayload;
import com.zxmr.action.ResultCode;
import com.zxmr.domain.Contract;
import com.zxmr.domain.SurgeryRecord;
import com.zxmr.domain.service.ContractService;
import com.zxmr.domain.service.SurgeryRecordService;
import com.zxmr.navigate.ExecuteException;
import com.zxmr.navigate.Executor;
import com.zxmr.util.ResourceManager;
import com.zxmr.util.Utils;

public class SurgeryRecordAction extends Executor{

	enum method{
		add,
		edit,
		query
	}
	@Override
	public void execute(Request request, Response response) {
		try
		{
			method method = Enum.valueOf(method.class, this.getElement().getMethod());
			switch(method)
			{
			case add:
				add(request,response);
				break;
			case edit:
				edit(request,response);
				break;
			case query:
				query(request,response);
				break;
			}

		}catch(Exception e)
		{
			ResponsePayload payload = new ResponsePayload();
			if(e instanceof ExecuteException)
			{
				payload.setValue(e.getMessage());
			}else
			{
				e.printStackTrace();
				payload.setValue("internal error");
			}
			response.setPayload(payload);
			response.setResult(ResultCode.COMPONENT_ERROR);
		}
	}
	private void add(Request request,Response response)throws ExecuteException
	{
		SurgeryRecord sr = parseAddRequest(request);
		checkField(sr,request);
		setFixFied(request,sr);
		
		SurgeryRecordService service = new SurgeryRecordService();
		if(!service.addSurgeryRecord(sr))
			this.internalErrorResponse(response);
	}
	private void setFixFied(Request request, SurgeryRecord sr)
	{
		sr.setCompany(request.getAccount().getCompany());
		sr.setCreateTime(Utils.getCurrentTime());
		sr.setCreateUser(request.getAccount().getName());
	}
	private void checkField(SurgeryRecord sr,Request request)throws ExecuteException
	{
		if(Utils.isStringEmptyWithTrim(sr.getTitle()))
				throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.surgery.record.notitle"));
		if(Utils.isStringEmptyWithTrim(sr.getSurgeryProcess()))
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.surgery.record.noprocess"));
		if(Utils.isStringEmptyWithTrim(sr.getSurgeryTime()))
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.surgery.record.notime"));
		if(Utils.isStringEmptyWithTrim(sr.getDoctor()))
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.surgery.record.nodoctor"));
		
		// check contract
		int contract = sr.getContract();
		if(contract <= 0)
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.surgery.record.nocontract"));
		
		ContractService contractService = new ContractService();
		Contract c = contractService.queryContract(contract,request.getAccount().getCompany());
		if(c == null)
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.surgery.record.contractNotExist"));
		
		sr.setCustomer(c.getCustomer());
	}
	private SurgeryRecord parseAddRequest(Request request)throws ExecuteException
	{
		try
		{
			return JSON.parseObject(request.getRequestParameters().get("parameters"), SurgeryRecord.class);
		}catch(Exception e)
		{
			throw new ExecuteException("invalid parameter");
		}
	}
	private void edit(Request request,Response response)throws ExecuteException
	{
		
	}
	private void query(Request request,Response response)throws ExecuteException
	{
		int id = Integer.parseInt(request.getRequestParameters().get("id"));
		
		SurgeryRecordService service = new SurgeryRecordService();
		SurgeryRecord sr = service.querySurgeryRecord(id);
		
		if(sr == null || sr.getCompany() != request.getAccount().getCompany())
			throw new ExecuteException(ResourceManager.getInstance().getMessage("com.zxmr.customer.noauth"));
		
		ResponsePayload payload = new ResponsePayload(JSON.toJSONString(sr));
		response.setPayload(payload);
	}
}
